import re
import os
from urllib import urlopen
from sugar.datastore import datastore
from sugar.activity import activity

class Book:
    ''' The class describes a book object '''
    book_id = ''
    xml_file_url = ''
    xml_file = ''
    book_url = ''
    book_txt = ''

    def get_xml(self):
        ''' get the xml description file '''
        # the xml description file is located at:
        # http://archive.org/serve/IDENTIFIER/IDENTIFIER_files.xml
        try:
            # debug
            # print "get_xml: trying to get: "+"http://archive.org/serve/"+self.book_id+"/"\
            #        +self.book_id+"_files.xml"

            self.xml_file = urlopen("http://archive.org/serve/"+self.book_id+"/"\
                    +self.book_id+"_files.xml").read()
        except IOError:
            # TODO: Error handling missing for the moment...
            print "getXML: invalid id or something else happened..."

    def get_xml_txt_file_url(self):
        ''' extracts the txt filename from the xml file '''

        # debug
        # print "get_xml_txt_file_url: Got:\n"+self.xml_file

        # TODO: pretty loose regular expression, the txt filename could be only
        # numbers 
        filename = re.search("\w+\.txt", self.xml_file)
        if filename.group():
            self.book_url = "http://archive.org/serve/"+self.book_id+"/"+filename.group()
        else:
            # TODO: Error handling missing for the moment
            print "getXMLTxtFileUrl: re not matching ..."

    def download_book(self):
        ''' downloads the actual txt file '''
        # Get the TXT ..
        try:
            self.book_txt = urlopen(self.book_url).read()
        except IOError:
            # TODO: Error handling missing for the moment...
            print "getBook: could not get the book..."

    def get_book(self, *args):
        ''' returns the txt '''
        return self.book_txt

    def parse_book(self, *args):
        ''' parses the book(pages, lines)... !? '''
        pass

    def __init__(self, entry_book_id, progressbar):
        if entry_book_id:
            # just testing the progressbar...
            self.progressbar = progressbar
            self.book_id = entry_book_id
            self.get_xml()
            # update progressbar
            self.progressbar.set_fraction(0.2)
            self.get_xml_txt_file_url()
            # update progressbar
            self.progressbar.set_fraction(0.4)
            self.download_book()
            # and again, update the progressbar 
            self.progressbar.set_fraction(0.6)

        else:
            # TODO: Error handling missing ... 
            print "Book.__init__(): no book_id supplied..."
            return None 

class Bookshelf:
    ''' The class describes a bookshelf object '''
    books = {}
    
    def add_book(self, book_id, book_object):
        ''' add a book to the bookshelf '''
        # TODO: not finished
        self.books[book_id] = book_object
    
    def get_book(self, book_id):
        ''' returns a book in the TextView '''
        # TODO: not finished
        pass

    def remove_book(self, book_id):
        ''' remove a book from the bookshelf '''
        # TODO: not finished
        self.books.pop(book_id)

    def __init__(self):
        # TODO: not finished
        pass

